package com.example.homework7;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;

public class BasicOperation extends Fragment {

    public BasicOperation(){
        super(R.layout.basic_operation);
    }

    private TextView contentTextView;
    private DbHelper dbHelper;
    private EditText Tusername;
    private EditText Tage;
    private String username;
    private String age;

    public void onViewCreated(@NonNull View view, @NonNull Bundle saveInstanceState){
        super.onViewCreated(view,saveInstanceState);

        dbHelper = new DbHelper(getContext());

        //增加一条数据
        Tage = getView().findViewById(R.id.AgeText);
        Tusername = getView().findViewById(R.id.NameText);

        Button AddButton = getView().findViewById(R.id.AddBotton);
        AddButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                username = Tusername.getText().toString();
                age = Tage.getText().toString();

                SQLiteDatabase db = dbHelper.getWritableDatabase();
                ContentValues values = new ContentValues();
                values.put("username",username);
                values.put("age",age);
                //参数1：表名
                //参数2：传null表示不能插入空的values（没有put任何东西）
                //参数3：要插入的数据
                //返回新创建行的id，如果创建行失败，返回-1
                long ret = db.insert("users",null,values);
                String msg = ret == -1?"添加失败!":"添加成功";
                Toast.makeText(getActivity(),msg,Toast.LENGTH_SHORT).show();
                db.close();
            }
        });


        contentTextView = getView().findViewById(R.id.contentview);
        //查询age<18
        Button AgeListButton = getView().findViewById(R.id.AgeListBotton);
        AgeListButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SQLiteDatabase db = dbHelper.getWritableDatabase();
                StringBuilder stringBuilder = new StringBuilder();
                Cursor cursor = db.query("users",null,"age<18",null,null,null,null);
                while (cursor.moveToNext()){
                    long id = cursor.getLong(cursor.getColumnIndex("id"));
                    String username = cursor.getString(cursor.getColumnIndex("username"));
                    int age = cursor.getInt(cursor.getColumnIndex("age"));
                    stringBuilder.append("        id: ").append(id)
                            .append("      username: ").append(username)
                            .append("      age: ").append(age)
                            .append("\n");
                }
                contentTextView.setText(stringBuilder.toString());
            }
        });


        //查询所有数据
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        StringBuilder stringBuilder = new StringBuilder();
        //参数1：表名
        //参数2：查询结果有包含哪些列，传null表示所有列
        //参数3：查询条件（where语句后面的条件），传null获取所有行
        //参数4：替换参数3中的问好
        //参数5：分组方式（group by语句后面的值）
        //参数6：having语句后面的条件
        //参数7：排序方式
        Cursor cursor = db.query("users",null,null,null,null,null,null);
        while (cursor.moveToNext()){
            long id = cursor.getLong(cursor.getColumnIndex("id"));
            String username = cursor.getString(cursor.getColumnIndex("username"));
            int age = cursor.getInt(cursor.getColumnIndex("age"));
            stringBuilder.append("        id: ").append(id)
                    .append("      username: ").append(username)
                    .append("      age: ").append(age)
                    .append("\n");
        }
        contentTextView.setText(stringBuilder.toString());


    }




}
